using System.Diagnostics;
using Seagull.Configuration;

namespace Seagull.DAL.Utils
{
    [DebuggerDisplay("{ToString()}")]
    public class FrameDataMinMaxCoercer : FrameDataCoercer
    {
        public static FrameDataCoercer Default
        {
            get
            {
                return new FrameDataMinMaxCoercer(
                Config.Instance.DataReadConfig.MinDataValue,
                Config.Instance.DataReadConfig.MaxDataValue);
            }
        }

        private readonly byte _min;
        private readonly byte _max;

        public FrameDataMinMaxCoercer(byte min, byte max)
        {
            _min = min;
            _max = max;
        }

        public override void Coerce(byte[] frameData)
        {
            for(var i = 0; i < frameData.Length; i++)
            {
                if(frameData[i] < _min)
                {
                    frameData[i] = _min;
                }
                else if(frameData[i] > _max && _max != default(byte))
                {
                    frameData[i] = _max;
                }
            }
        }

        public override string ToString()
        {
            return string.Format("Min: {0}, Max: {1}", _min, _max);
        }
    }
}